Skip to content

Fix multithreaded wasm crash (solves #164)#165

Merged
josephlr merged 5 commits intorust-random:masterfrom
chemicstry:wasm_thread_issue
Oct 26, 2020
Merged

Fix multithreaded wasm crash (solves #164)#165
josephlr merged 5 commits intorust-random:masterfrom
chemicstry:wasm_thread_issue

Conversation

@chemicstry
Copy link
Copy Markdown
Contributor

This solves #164 by using a preallocated Uint8Array for calling crypto.getRandomValues.

The stdweb implementation seems fine, as it already uses Uint8Array.

Should this fix be backported to 0.15? There are a lot of crates that still use that version.

Comment thread src/wasm-bindgen.rs
@chemicstry
Copy link
Copy Markdown
Contributor Author

The CI seems to have failed due to unrelated reason. Can someone rerun?

Copy link
Copy Markdown
Member

@josephlr josephlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, a few minor changes. Also, now that we are depending on js-sys can you add a TODO where we use Global and Self_ to replace that functionality with js_sys::global()

Comment thread src/wasm-bindgen.rs Outdated
Comment on lines +19 to +24
struct BrowserCryptoContext {
crypto: BrowserCrypto,
// A temporary buffer backed by browser memory to avoid multithreaded wasm exception. See issue #164
buf: Uint8Array,
}

Copy link
Copy Markdown
Member

@josephlr josephlr Oct 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having this structure, we could just do:

enum RngSource {
    Node(NodeCrypto),
    Browser(BrowserCrypto, Uint8Array),
}

and then add the comment about using the temporary buffer when we actually use the temp buffer:

RngSource::Browser(crypto, buf) => {
    // getRandomValues does not work with all types of WASM memory, so
    // we initially write to browser memory to avoid an exception.
    for chunk in dest.chunks_mut(BROWSER_CRYPTO_BUFFER_SIZE) {
        ...
    }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late response, but do you still want me to fix this? Since you force pushed to my branch and already approved it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fixing this would be preferable (I can also do it when i have time).

Comment thread src/wasm-bindgen.rs Outdated
Comment on lines +76 to +80
let buf = Uint8Array::new_with_length(BROWSER_CRYPTO_BUFFER_SIZE as u32);

let ctx = BrowserCryptoContext { crypto, buf };

return Ok(RngSource::Browser(ctx));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This then becomes:

let buf = Uint8Array::new_with_length(BROWSER_CRYPTO_BUFFER_SIZE as u32);
return Ok(RngSource::Browser(crypto, buf));

Comment thread src/wasm-bindgen.rs
@josephlr
Copy link
Copy Markdown
Member

Should this fix be backported to 0.15? There are a lot of crates that still use that version.

Ya that should be fine, once we make sure this works, I'll backport it (maybe once we also cleanup the Self usage).

chemicstry and others added 2 commits October 23, 2020 14:49
Signed-off-by: Joe Richey <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
@josephlr josephlr merged commit c29dd5f into rust-random:master Oct 26, 2020
josephlr added a commit to josephlr/getrandom that referenced this pull request Oct 28, 2020
Solves rust-random#164 for the v0.1 branch

Signed-off-by: Joe Richey <joerichey@google.com>
newpavlov pushed a commit that referenced this pull request Dec 7, 2020
Solves #164 for the v0.1 branch
takumi-earth pushed a commit to earthlings-dev/getrandom that referenced this pull request Jan 27, 2026
Signed-off-by: Joe Richey <joerichey@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants